package cn.zhl.base.enums;

import java.util.ArrayList;
import java.util.List;

/**
 * 返回码枚举
 */
public enum ResultCode {
	/* 成功状态码 */
	SUCCESS(0, "成功"),

	/**
	 * 错误
	 */
	FAIL(-1, "操作失败！"),

	/* 失败返回码400开头 */
	FAIL_WRONG_PWD(400, "用户名或者密码错误"),
	FAIL_UNAUTHORIZED(401, "请先登录！"),
	FAIL_EXISTS(402, "数据重复！"),
	FAIL_DENY(403, "无权限"),
	FAIL_NOT_FOUND(404, "未找到"),
	/**
	 * 请求方式不支持！
	 */
	FAIL_BAD_METHOD(405, "请求方式不支持！"),

	/**
	 * 服务器内部错误
	 */
	/**
	 * 操作异常！
	 */
	ERROR_INTERVAL(500, "操作异常！"),

	/* 参数错误：10001-19999 */
	PARAM_IS_INVALID(10001, "参数无效"),
	PARAM_IS_BLANK(10002, "参数为空"),
	PARAM_TYPE_BIND_ERROR(10003, "参数类型错误"),
	PARAM_NOT_COMPLETE(10004, "参数缺失"),

	/* 权限错误：70001-79999 */
	PERMISSION_NO_ACCESS(70001, "无访问权限");

	private Integer code;

	private String message;

	ResultCode(Integer code, String message) {
		this.code = code;
		this.message = message;
	}

	public static String getMessage(String name) {
		for (ResultCode item : ResultCode.values()) {
			if (item.name().equals(name)) {
				return item.message;
			}
		}
		return name;
	}

	public static Integer getCode(String name) {
		for (ResultCode item : ResultCode.values()) {
			if (item.name().equals(name)) {
				return item.code;
			}
		}
		return null;
	}

	//校验重复的code值
	public static void main(String[] args) {
		ResultCode[] ApiResultCodes = ResultCode.values();
		List<Integer> codeList = new ArrayList<Integer>();
		for (ResultCode ApiResultCode : ApiResultCodes) {
			if (codeList.contains(ApiResultCode.code)) {
				System.out.println(ApiResultCode.code);
			} else {
				codeList.add(ApiResultCode.code());
			}
		}
	}

	public Integer code() {
		return this.code;
	}

	public String message() {
		return this.message;
	}

	@Override
	public String toString() {
		return String.format(" Status:{code=%s, message=%s} ", code(), message());
	}
}
